package com.stsdai.manager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stsdai.common.pojo.StsdaiResult;
import com.stsdai.manager.mapper.ItemMapper2;
import com.stsdai.manager.pojo.Item;
import com.stsdai.manager.pojo.ItemDesc;
import com.stsdai.manager.service.ItemDescService;
import com.stsdai.manager.service.ItemService;

@Service
public class ItemServiceImpl extends BaseServiceImpl<Item> implements ItemService {

	@Autowired
	private ItemDescService itemDescService;
	@Resource
	private ItemMapper2 itemMapper2;

	private static final ObjectMapper MAPPER = new ObjectMapper();
	
	@Override
	public void saveItem(Item item, String desc) {
		// 保存商品基础信息
		item.setStatus(1);
		super.save(item);

		// 保存商品描述信息
		ItemDesc itemDesc = new ItemDesc();
		itemDesc.setItemId(item.getId());
		itemDesc.setItemDesc(desc);

		this.itemDescService.save(itemDesc);
	}

	@Override
	public StsdaiResult<Item> queryItemByShopId(Long shopid, Integer page, Integer rows) {
		// TODO Auto-generated method stub
		// 设置分页数据
		PageHelper.startPage(page, rows);
		// 设置查询条件
		Item item = new Item();
		item.setShopid(shopid);
		// 执行查询
		List<Item> list = super.queryListByWhere(item);
		
		// 封装返回对象
		StsdaiResult<Item> stsdaiResult = new StsdaiResult<>();
		
		// 设置结果集rows
		stsdaiResult.setRows(list);
		// 设置总记录数total
		PageInfo<Item> pageInfo = new PageInfo<>(list);
		stsdaiResult.setTotal(pageInfo.getTotal());
		
		return stsdaiResult;
	}
	
	@Override
	public StsdaiResult<Item> queryItemByPage(Integer page, Integer rows) {
		// TODO Auto-generated method stub
		// 设置分页数据
		PageHelper.startPage(page, rows);

		// 执行查询
		List<Item> list = super.queryAll();

		// 封装返回对象
		StsdaiResult<Item> stsdaiResult = new StsdaiResult<>();

		// 设置结果集rows
		stsdaiResult.setRows(list);
		// 设置总记录数total
		PageInfo<Item> pageInfo = new PageInfo<>(list);
		stsdaiResult.setTotal(pageInfo.getTotal());

		return stsdaiResult;
	}

	@Override
	public List<Item> queryNewItem() {
		// 创建容器存放所有的商品分类信息
		/*List<Map<String, Object>> result = new ArrayList<>();*/
		
		// 执行查询
		List<Item> list = itemMapper2.queryNewItem();
		
		// 声明存放广告数据的List《Map》容器
		List<Map<String, Object>> results = new ArrayList<>();
		
		// 遍历结果集，把查询的内容数据封装到list《Map》中
		/*for (Item item : list) {
			
			// 声明Map
			Map<String, Object> map = new HashMap<>();
			//给map进行注入值
			map.put("id", item.getId());
			map.put("title", item.getTitle());
			map.put("sellPoint", item.getSellPoint());
			map.put("price", item.getPrice());
			map.put("image", item.getImage());
			
			//放入容器
			result.add(map);
		}
		
		String json = "";
		try {
			json = MAPPER.writeValueAsString(result);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		
		return list;	
	}

	@Override
	public List<Item> queryYouJiCai() {
		List<Item> list = itemMapper2.queryYouJiCai();
		
		return list;
	}

	@Override
	public List<Item> queryShiLingGuo() {
		List<Item> list = itemMapper2.queryShiLingGuo();
		return list;
	}
	
	

}
